<?php

namespace CNEPAdmin\Controller;

use CNEPAdmin\Controller\CommonController;

class ShopController extends CommonController {

    protected $_modelName = "Shop";

    protected function setCode() {
        $this->_code = "Shop";
    }

    public function index() {
        $this->display();
    }

    /**
     * 得到条件查询数组
     * @return Array
     */
    protected function _getQueryArr(&$where, &$order) {
        $where = array();
        if (strlen(trim(I("sShopName"))) > 0) {
            $where['shopName'] = array("like", "%" . trim(I("sShopName") . "%"));
        }
        if (intval(trim(I("sIsWork", -1))) >= 0) {
            $where['isWork'] = intval(I("sIsWork"));
        }
        if (strlen(trim(I("sAddress"))) > 0) {
            $where['address'] = array("like", "%" . trim(I("sAddress") . "%"));
        }
        if (intval(trim(I("shopType", 1))) > 0) {
            $where['shopType'] = I("shopType", 1);
        }
        $_REQUEST['shopType'] = intval(I("shopType", 1));

        $where['isOn'] = 1;
//        $where['isWork'] = I("isWork", 1);
//        $_REQUEST['isWork'] = I("isWork", 1);

        $orderBy = 'addTime';
        $desc = 'desc';
        if (intval(I("sDesc")) === 1) {
            $desc = 'asc';
        }
        if (strlen(trim(I("sOrderBy", ''))) > 0) {
            $orderBy = I("sOrderBy");
        }

        $order = array(
            $orderBy => $desc
        );
    }

    /**
     * 商店列表
     */
    public function shopList() {
        $this->checkPriv('shopMsg', 1, false);

        $where = array();
        $order = array();
        $this->_getQueryArr($where, $order);

        $this->_setModel();

        $count = $this->_model->getShopListLength($where);
        if ($count > 0) {
            $psize = I("psize", 15);
            $list = $this->_model->getShopList(I("p", 1), $psize, $where, $order);
            $this->assign('list', $list);
            $Page = new \Think\Page($count, $psize); // 实例化分页类 传入总记录数和每页显示的记录数
            $Page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $show = $Page->show(); // 分页显示输出
            $this->assign('num', $Page->firstRow); // 赋值分页输出
            $this->assign('page', $show); // 赋值分页输出
            //
            $this->assign("shopType", I("shopType", 1));
//            $this->assign('test',  dump($Page));
        }

        $this->assign('lastsql', $this->_model->getLastSql() . "count:$count");
        $this->assign('p', I("p"));

        $this->display();
    }

    private function _dealShopList(&$list) {
        $shopIdArr = array();

        if ($list) {
            foreach ($list as $val) {
                $shopIdArr[] = $val['shopID'];
            }
            $tmpData = array(
                "shopID" => array("in", $shopIdArr)
            );
            $tmpOrder = array(
                'goodsID' => 'asc'
            );

            $goodsList = D("Goods")->getGoodsList(1, 15, $tmpData, $tmpOrder);
            if ($goodsList) {
                $i = 0;
                foreach ($list as $value) {
                    $list[$i]['goodsList'] = array();

                    foreach ($goodsList as $v) {

                        if (intval($value['shopID']) === intval($v['shopID'])) {
                            $list[$i]['goodsList'][] = $v;
                        }
                    }
                    $i ++;
                }
            }
        }
    }

    /**
     * 商店信息 
     */
    public function shopMsg() {
        $this->checkPriv('shopMsg', 1, false);
        $shopId = intval(I("shopID", 0));

        if ($shopId > 0) { // 编辑
            $arr = D("Shop")->getShop(null, $shopId);
            $this->assign("arr", $arr);
            $this->assign('oilPrice',json_decode($arr['oilPriceJson'],true));
			
            // 如果是油站，取得油价json
            if (intval($arr['shopType']) === 1) {
                $oilPriceJson = $arr['oilPriceJson'];
//             	dump($oilPriceJson);
                if (strlen(trim($oilPriceJson)) > 0) {
                    $oilPriceJson = json_decode($oilPriceJson, true);

                    $this->assign('oilPriceJson', $oilPriceJson);
                }
            }

            // 得到所有类型（commentType=1）的券
            $typeList = D("GoodsCat")->where(array('isOn' => 1, 'commentType' => 1))->select();
            $this->assign('typeList', $typeList);

            $glist = D("Goods")->getGoodsList(1, 20, array("shopID" => $shopId, "isOn" => 1), array('goodsID' => 'asc'));
            $this->assign("goodsList", $glist);
        }
        $this->assign("empty", "<a href=\"javascript:void(0);\" class=\"btn btn-default \" style=\"margin-bottom:10px\" id=\"add_fushu\" >添加附属商品</a>");
        $static = C("TMPL_PARSE_STRING");
        $showPic = strlen($arr['headImg']) > 0 ? $arr['headImg'] : $static['__STATICS__'] . "/images/upload_comn.png";
        $this->assign("showPic", $showPic);
        $this->assign('imgDomain',C('IMG_DOMAIN'));
        $this->display();
    }

    /**
     * 保存信息（添加和修改）
     */
    public function saveShop() {
        $this->checkPriv('shopMsg', 2);

        $sid = I("post.shopID", 0);
        $this->_setModel();
        $shop = $this->_model;
        
        !empty($_POST['headImg'])?$_POST['headImg']=$this->get_url_path(I('post.headImg')):"";
        !empty($_POST['thumb'])?$_POST['thumb']=$this->get_url_path(I('post.thumb')):"";
        $data = $_POST;
        $data['oilPriceJson'] = json_encode($_POST['oilPriceJson']);
	
        if (intval($sid) > 0) {
            $rt = $shop->updateShop($data);
        } else {
            $rt = $shop->addShop($data);
        }
        $rejson = $rt ? array("result" => 1, "desc" => $rt) : array("result" => 0, "desc" => $this->_model->getError() . $this->_model->getDbError());
        $this->ajaxReturn($rejson);
    }

    public function deleteShop() {
        $this->checkPriv('shopMsg', 2);
        $sid = I("shopID", 0);

        if ($sid <= 0) {
            $rejson = array(
                "result" => 0,
                "desc" => "无效ID"
            );
        } else {
            $shop = D("Shop");
            if ($shop->deleteShop($sid)) {
                $rejson = array(
                    "result" => 1
                );
            } else {
                $rejson = array(
                    "result" => 0,
                    "desc" => $shop->getError()
                );
            }
        }

        $this->ajaxReturn($rejson);
    }

}
